﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                timer1.Enabled = true;
                label5.Text = "用户名不能为空";
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
                label5.Text = null;
                label6.Text = null;
                label4.Text = null;
                timer1.Enabled = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var gb = MessageBox.Show("是否取消注册！", "取消注册", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (gb == DialogResult.OK)
            {
                this.Close();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text) && string.IsNullOrEmpty(textBox3.Text))
            {
                timer1.Enabled = true;
                label5.Text = "用户名不能为空";
                label6.Text = "密码不能为空";
                label4.Text = "确认密码不能为空";
                textBox1.Focus();
            }
            else if(string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
            {
                timer1.Enabled = true;
                label5.Text = "用户名不能为空";
                label6.Text = "密码不能为空";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox3.Text))
            {
                timer1.Enabled = true;
                label5.Text = "用户名不能为空";
                label4.Text = "确认密码不能为空";
                textBox1.Focus();

            }
            else if (string.IsNullOrEmpty(textBox2.Text) && string.IsNullOrEmpty(textBox3.Text))
            {
                timer1.Enabled = true;
                label6.Text = "密码不能为空";
                label4.Text = "确认密码不能为空";
                textBox2.Focus();
            }
            else if (string.IsNullOrEmpty(textBox1.Text))
            {
                timer1.Enabled = true;
                label5.Text = "用户名不能为空";
                textBox1.Focus();
            }
            else if (string.IsNullOrEmpty(textBox2.Text))
            {
                timer1.Enabled = true;
                label6.Text = "密码不能为空";
                textBox2.Focus();
            }
            else if (string.IsNullOrEmpty(textBox3.Text))
            {
                timer1.Enabled = true;
                label4.Text = "确认密码不能为空";
                textBox3.Focus();
            }
            else
            {
                if (Form1.User.ContainsKey(textBox1.Text))
                {
                    label5.Text = "用户名已存在";
                    textBox1.SelectAll();
                    textBox1.Focus();
                }
                else if(textBox2.TextLength < 6)
                {
                    label6.Text = "密码不足6位";
                    textBox2.Focus();
                }
                else if (!textBox3.Text.Equals(textBox2.Text))
                {
                    label4.Text = "两次密码输入不一致";
                    textBox3.Text = null;
                    textBox3.Focus();
                }
                else
                {
                    Form1.User.Add(textBox1.Text,textBox2.Text);
                    if (Form1.User.ContainsKey(textBox1.Text) && Form1.User[textBox1.Text].Equals(textBox2.Text))
                    {
                        var gb = MessageBox.Show($"注册成功！\n现在可以使用用户名：{textBox1.Text}进行登录\n按确认可返回登录", "注册", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                        if (gb == DialogResult.OK)
                        {
                            this.Close();
                        }
                    }
                }
            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (!textBox3.Text.Equals(textBox2.Text))
            {
                label4.Text = "两次密码输入不一致";
            }
            else
            {
                label4.Text = null;
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.TextLength<6)
            {
                label6.Text = "密码不足6位";
            }
            else
            {
                label6.Text = null;
            }
        }

        private void textBox3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
            {
                timer1.Enabled = true;
                label5.Text = "用户名不能为空";
                label6.Text = "密码不能为空";
            }
            else if (!string.IsNullOrEmpty(textBox1.Text) && string.IsNullOrEmpty(textBox2.Text))
            {
                timer1.Enabled = true;
                label6.Text = "密码不能为空";
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if ( Form1.User.ContainsKey(textBox1.Text) )
            {
                label5.Text = "用户名已存在";
            }
            else
            {
                label5.Text = null;
            }
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            textBox1.Focus();
        }
    }
}
